package vn.njai.thousand_english_word.Activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.MenuItem;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.astuetz.PagerSlidingTabStrip;
import vn.njai.thousand_english_word.R;

/**
 * Created by VUONG on 12/14/2014.
 */
public class ProgressDetailActivity extends ActionBarActivity {
    private String TAG = "ProgressDetailActivity";
    private Context mContext;
    public final Handler handler = new Handler();

    @InjectView(R.id.toolbar)
    public Toolbar mToolbar;

    @InjectView(R.id.tabs)
    public PagerSlidingTabStrip tabs;

    @InjectView(R.id.pager)
    public ViewPager pager;

    private MyPagerAdapter adapter;

    public static void start(Context context) {
        Intent intent = new Intent(context,ProgressDetailActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress_detail);
        mContext = getApplicationContext();
        init();
    }

    public void init(){
        ButterKnife.inject(this);
        initActionBar();
        adapter = new MyPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);
        final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
        pager.setPageMargin(pageMargin);
        tabs.setViewPager(pager);
        Typeface face=Typeface.createFromAsset(mContext.getAssets(),mContext.getString(R.string.font_name));
        tabs.setTypeface(face,Typeface.BOLD);
    }

    private void initActionBar(){
        mToolbar.setTitle(getString(R.string.actionbar_title_progress_detail));
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_back);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        private final String[] TITLES = {
                getString(R.string.word_type_learned),
                getString(R.string.word_type_twotime),
                getString(R.string.word_type_onetime),
                getString(R.string.word_type_never)
        };

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public Fragment getItem(int position) {
            return ProgressDetailFragment.newInstance(mContext,position);
        }

    }
}
